//    Copyright (C) Kherty.  All rights reserved.
using System;
using System.Collections.Generic;
using System.Text;
using OpenLS.Spreadsheet.Expressions;
using OpenLS.Spreadsheet.Extensibility;
using OpenLS.Spreadsheet.Parsing;

namespace OpenLS.Spreadsheet
{
    partial class Worksheet: IWorksheet
    {
        IWorkbook IWorksheet.Workbook { get { return Workbook; } }
       
        #region IWorksheet Members

        T IWorksheet.ChangeType<T>(object v)
        {
            return this.Workbook.ChangeType<T>(v);
        }

        object IWorksheet.Info(string type_text)
        {
            switch (type_text)
            {
                case "directory":
                    return Environment.CurrentDirectory;
                case "numfile":
                    return this.Workbook.Sheets.Count;
                case "origin":
                    {
                        var cell = this[TopLeftPane.Top, TopLeftPane.Left];
                        var cellRef = new StaticCellReference(new AnchoredRowReference(cell.Row, true),
                                                              new AnchoredColumnReference(cell.Column, true));
                        return "$A:" + cellRef.Label;
                    }
                case "osversion":
                    return Environment.OSVersion.VersionString;
                case "recalc":
                    switch (Workbook.CalculationProperties.CalculationMode)
                    {
                        case CalculationMode.Automatic:
                        case CalculationMode.AutomaticNoTable:
                            return "Automatic";
                        case CalculationMode.Manual:
                            return "Manual";
                        default:
                            throw new NotSupportedException();
                    }
                case "release":
                    return "Spreadsheet";
                case "system":
                    return "pcdos";
            }
            return Errors.ValueError;

        }

        #endregion

        #region IWorksheet Members


        object IWorksheet.CreateHyperlink(string location, string friendly_name)
        {
            Hyperlink l = new Hyperlink();
            if (friendly_name != null)
                l.Display = friendly_name;
            else
                l.Display = location;
            l.ExternalUri = new Uri(location, UriKind.RelativeOrAbsolute);
            return l;
        }

        #endregion

        #region IWorksheet Members


        object IWorksheet.GetRange(string ref_text, bool A1)
        {
            RangeReference rr = GetRangeReference(ref_text);
            if (rr != null)
                return rr.Range;
            return Errors.RefError;

        }

        #endregion

        #region IWorksheet Members


        IRange IWorksheet.CreateRange(ICell cell1, ICell cell2)
        {
            return new MultiCellsRange((Cell) cell1,(Cell) cell2);
        }

        #endregion

        #region IWorksheet Members


        object IWorksheet.Address(int row_num, int column_num, int abs_num, bool A1, string sheet_text)
        {
            if (row_num < 1 || column_num < 1)
                return Errors.ValueError;
            CellNamingConvention c;
            if (A1)
                c = CellNamingConventions.A1NamingConvention;
            else
                c = CellNamingConventions.R1C1NamingConvention;
            bool lockRows;
            bool lockColumns;
            switch (abs_num)
            {
                case 1:
                    lockColumns = true;
                    lockRows = true;
                    break;
                case 2:
                    lockColumns = false;
                    lockRows = true;
                    break;
                case 3:
                    lockColumns = true;
                    lockRows = false;
                    break;
                case 4:
                    lockColumns = false;
                    lockRows = false;
                    break;
                default:
                    throw new NotSupportedException();
            }
            CellReferenceToken token = new CellReferenceToken(row_num - 1, lockRows, column_num - 1, lockColumns);
            string result = c.GetText(token);
            //string result = c.Address(row_num - 1, column_num - 1, lockRows, lockColumns);
            if (sheet_text != null)
                result = Worksheet.GetSafeName(sheet_text) + "!" + result;
            return result;
        }

        public object GetPivotData(string field, IRange range, params object[] fieldAndItems)
        {
            if (range == null)
                return Errors.ValueError;
            if (field == null)
                return Errors.ValueError;
            Cell cell = (Cell) range.FirstCell;
            var table = cell.PivotTable;
            if (table == null)
                return Errors.RefError;
            return table.GetPivotData(field, fieldAndItems);
        }

        public DateTimeOrigin DateTimeOrigin
        {
            get {
                var prop = Workbook.Properties;
                if (prop != null)
                    return prop.DateTimeOrigin;
                return DateTimeOrigin.Origin1900;
            }
        }

        #endregion
    }
}
